package com.evolution.warlock.dao.impl;

import java.util.List;

import javax.persistence.FlushModeType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.evolution.warlock.dao.CivilDisputeDao;
import com.evolution.warlock.entity.CivilDispute;

@Repository("civilDisputeDaoImpl")
public class CivilDisputeDaoImpl extends BaseDaoImpl<CivilDispute, Long> implements CivilDisputeDao{

	public CivilDispute findCivilDispute(Long criminalNumber) {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<CivilDispute> criteriaQuery = criteriaBuilder.createQuery(CivilDispute.class);
		Root<CivilDispute> root = criteriaQuery.from(CivilDispute.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();
		if(criminalNumber != null){
		   restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("penaltyNumber"), criminalNumber));
		}
	    criteriaQuery.where(restrictions);
	    List<CivilDispute>  civilDisputeList = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
	    if(null!=civilDisputeList && civilDisputeList.size()>0){
	    	return civilDisputeList.get(0);
	    }else{
	    	return null;
	    }
	}
}
